package com.qf.util;

import java.util.Properties;

/*
 *@Description:
 *@author:刘哥
 *@Date:10:03 2022/7/8
 */
public class BeanFactory {
    private Properties properties =new Properties();

    public BeanFactory() {
    }
    public BeanFactory(String config)throws Exception {
        //加载配置文件                           properties=config=bean.properties
        properties.load(BeanFactory.class.getResourceAsStream(config));
    }
    public  Object getBean(String beanName) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        //获取配置路径                    通过usersercice(beanName)获取 bean.properties里面的 值
        String classPath = properties.getProperty(beanName);//userService=com.qf.service.impl.UserServiceImpl
        if (classPath != null) {
            //反射加载类对象
            Class clazz = Class.forName(classPath);
            //返回反射对象
            return clazz.newInstance();
        }
        return null;
    }

    /*public static Object createBean(String className){
        try {
            return Class.forName(className).newInstance();
        } catch (Exception e) {
            e.printStackTrace();
            throw  new RuntimeException(e.getMessage());
        }
    }*/
}